package dao;

import java.sql.Connection;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * @deprecated Usar GenericDaoImpl
 */
@Deprecated
public class AbstractDao {

	private Session session;

	public Connection getConnection() throws DaoException {
		return ConnectionSingleton.getInstance();
	}

	public Session getSession() throws DaoException {
		if (session == null || !session.isOpen()) {
			session = SessionFactorySingleton.getInstance().openSession(
					getConnection());
			session.beginTransaction();
		}
		return session;
	}

	public void close() throws DaoException {
		try {
			
			if (session != null && session.isOpen()) {
				Transaction t = session.getTransaction();
				if (t.isActive() && !t.wasCommitted()) {
					t.commit();
				}
				session.close();
			}
		} catch (HibernateException e) {
			throw new DaoException(e);
		}
	}

	public void criar(Object obj) throws DaoException {
		getSession().save(obj);
	}

}
